
import os
import launch
import launch.launch_description_sources
import launch_ros
from ament_index_python.packages import get_package_share_directory # 获取share目录的绝对路径

def generate_launch_description():
    # 声明一个launch的参数
    action_declare_start_rqt = launch.actions.DeclareLaunchArgument(
        'start_rqt',
        default_value='false',
        description='Start rqt')
    strat_rqt = launch.substitutions.LaunchConfiguration('start_rqt',default='False')

    # 动作1，在一个launch中调用其他launch
    multisim_launch_path = [os.path.join(get_package_share_directory('turtlesim'), 'launch', 'multisim.launch.py')]
    action_include_launch = launch.actions.IncludeLaunchDescription(
       launch.launch_description_sources.PythonLaunchDescriptionSource(
           multisim_launch_path
       )
    )
    # 动作2打印数据
    action_log_info = launch.actions.LogInfo(msg=str(multisim_launch_path))

    # 动作3,执行进程，（执行一条命令）ros2 topic list
    action_topic_list = launch.actions.ExecuteProcess(
        cmd=['ros2', 'topic', 'list','-t'],
        output='screen'
    )
    action_start_rqt = launch.actions.ExecuteProcess(
        condition = launch.conditions.IfCondition(strat_rqt),
        cmd=['rqt'],
        output='screen'
    )
    # 动作4,组织动作成组，将多个动作放在一组
    action_group = launch.actions.GroupAction([
        # 动作5-定时器
        launch.actions.TimerAction(period=2.0, actions=[action_include_launch]),
        launch.actions.TimerAction(period=3.0, actions=[action_topic_list]),
        launch.actions.TimerAction(period=4.0, actions=[action_start_rqt]),
    ])
   

    return launch.LaunchDescription([
        action_declare_start_rqt,
        action_log_info,
        action_group
       
    ])